﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MINERP.DAL;
using MINERP.COM;
using MINERP.BLL.Common;
using MINERP.MODEL.FoundationModel;
using MINERP.MODEL.PrintManage;


namespace MINERP.BLL.PrintManage
{
    public class PaperBLL : IDataAccess
    {
        public PaperInfoModel GetPaper(int paperId)
        {
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                PaperInfo paper = entities.PaperInfo.Include("printer").FirstOrDefault(p => p.PrintPaperId == paperId);
                PaperInfoModel paperInfo = MappingPaperToModel(paper);
                return paperInfo;
            }

        }
        /// <summary>
        /// 获得正在使用状态的打印纸
        /// </summary>
        /// <returns></returns>
        public PaperInfoModel GetUsingPaper(int printerid)
        {
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                PaperInfo paper = entities.PaperInfo.Include("printer").FirstOrDefault(p => p.Status == (int)PaperInfoStatus.Using && p.printer.PrinterID == printerid);
                PaperInfoModel paperInfo = MappingPaperToModel(paper);
                return paperInfo;
            }
        }
        /// <summary>
        /// 校正纸长
        /// </summary>
        /// <param name="printer"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public int CalibrationPaperLength(Printer_Info printer, double length, bool isAdd)
        {
            int ret = 0;
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                try
                {
                    PaperInfo paper = entities.PaperInfo.FirstOrDefault(p => p.Status == (int)PaperInfoStatus.Using && p.printer.PrinterID == printer.PrinterID);
                    if (isAdd)
                    {
                        paper.Length += length;
                        paper.LastLength += length;
                    }
                    else
                    {
                        if (paper.LastLength >= length)
                        {
                            paper.Length -= length;
                            paper.LastLength -= length;
                        }
                        else
                        {
                            ret = -2;
                        }
                    }
                    entities.SaveChanges();
                }
                catch
                {
                    ret = -1;
                }
            }
            return ret;

        }

        private PaperInfoModel MappingPaperToModel(PaperInfo paper)
        {
            PaperInfoModel paperInfo = new PaperInfoModel();
            if (paper != null)
            {
                if (!paper.paperTypeReference.IsLoaded)
                {
                    paper.paperTypeReference.Load();
                }
                if (!paper.paperFaceReference.IsLoaded)
                {
                    paper.paperFaceReference.Load();
                }
                paperInfo.AddTime = (DateTime)paper.AddTime;
                paperInfo.EndPaperLength = (double)paper.EndPaperLength;
                paperInfo.LastBordId = paper.LastBordId;
                paperInfo.LastLength = (double)paper.LastLength;
                paperInfo.LastPrintTime = (DateTime)paper.LastPrintTime;
                paperInfo.Length =(double) paper.Length;
                paperInfo.PaperFaceID =(int) paper.paperFace.PaperFaceID;
                paperInfo.PaperFaceName = paper.paperFace.Name;
                paperInfo.PaperTypeID = paper.paperType.PaperTypeID;
                paperInfo.PaperTypeName = paper.paperType.Name;
                if (paper.printer != null)
                {
                    paperInfo.PrinterID = paper.printer.PrinterID;
                }
                paperInfo.PrintPaperId = paper.PrintPaperId;
                paperInfo.Remark = paper.Remark;
                paperInfo.Status =(int) paper.Status;
                paperInfo.InvalidationArea = paper.PaperBreakArea;
                paperInfo.ValidationArea = paper.PaperYieldArea;
                paperInfo.PaperConsumptionLength = paper.PaperConsumptionLength;
                switch (paper.Status)
                {
                    case (int)PaperInfoStatus.Complete:
                        paperInfo.StatusName = "标志完成";
                        break;
                    case (int)PaperInfoStatus.New:
                        paperInfo.StatusName = "未使用";
                        break;
                    case (int)PaperInfoStatus.Used:
                        paperInfo.StatusName = "使用过";
                        break;
                    case (int)PaperInfoStatus.Using:
                        paperInfo.StatusName = "正在使用";
                        break;
                }
                paperInfo.Width =(double) paper.Width;
            }
            else
            {
                paperInfo = null;
            }
            return paperInfo;
        }
        /// <summary>
        /// 换纸
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public int ChangePaper(PaperInfoModel paperInfo, Printer_Info printerInfo, double paperTitle, out int paperWasteRecordsId, out PaperInfoModel prePaperInfo)
        {
            prePaperInfo = new PaperInfoModel();
            paperWasteRecordsId = 0;
            int ret = 0;
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                try
                {
                    PaperInfo prePaper = entities.PaperInfo.FirstOrDefault(p => p.Status == (int)PaperInfoStatus.Using && p.printer.PrinterID == printerInfo.PrinterID);
                    if (prePaper != null)
                    {
                        prePaper.Status = (int)PaperInfoStatus.Used;
                        prePaperInfo.Length =(double) prePaper.Length;
                        prePaperInfo.LastLength = (double)prePaper.LastLength;
                        prePaperInfo.PrintPaperId = prePaper.PrintPaperId;
                        prePaperInfo.Width =(double) prePaper.Width;
                        prePaperInfo.LastPrintTime = (DateTime)prePaper.LastPrintTime;
                        prePaperInfo.ValidationArea = prePaper.PaperYieldArea;
                        prePaperInfo.InvalidationArea = prePaper.PaperBreakArea;
                        prePaperInfo.PaperConsumptionLength = prePaper.PaperConsumptionLength;
                        prePaperInfo.LastBordId = prePaper.LastBordId;
                    }
                    else
                    {
                        prePaperInfo = null;
                    }
                    PaperInfo newPaper = entities.PaperInfo.FirstOrDefault(p => p.PrintPaperId == paperInfo.PrintPaperId);
                    newPaper.Status = (int)PaperInfoStatus.Using;
                    newPaper.LastLength = paperInfo.LastLength;
                    newPaper.printer =entities.printer.FirstOrDefault(p=>p.PrinterID== printerInfo.PrinterID);


                    //记录纸头
                    //MINERP.DAL.PaperWasteType wasteType = entities.PaperWasteType.FirstOrDefault(p => p.PaperOperationTypeName == "纸头");
                    //if (wasteType == null)
                    //{
                    //    wasteType = new MINERP.DAL.PaperWasteType();
                    //    wasteType.PaperOperationTypeName = "纸头";
                    //}
                    
                    //PaperWasteRecords wasteRecord = new PaperWasteRecords();
                    //wasteRecord.CreateTime = UnityMethod.GetSystemTime();
                    //wasteRecord.Length = paperTitle;
                    //wasteRecord.PaperInfo = newPaper;
                    //wasteRecord.Status = 0;
                    //wasteRecord.Width =(double) newPaper.Width;
                    //wasteRecord.PaperWasteType = wasteType;
                    //wasteRecord.PrinterWaste = true;
                    //entities.AddToPaperWasteRecords(wasteRecord);

                    entities.SaveChanges();


                    //paperWasteRecordsId = wasteRecord.Id;
                    paperWasteRecordsId = 0;
                }
                catch
                {
                    ret = -1;
                }
            }
            return ret;
        }

        #region IDataAccess 成员
        /// <summary>
        /// 添加纸
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public int Insert(object o)
        {
            int ret = 0;
            PaperInfoModel paperInfo = (PaperInfoModel)o;
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                try
                {
                    PaperInfo paper = new PaperInfo();
                    paper.AddTime = paperInfo.AddTime;
                    paper.EndPaperLength = paperInfo.EndPaperLength;
                    paper.PaperBreakArea = paperInfo.InvalidationArea;
                    paper.LastBordId = paperInfo.LastBordId;
                    paper.LastLength = paperInfo.LastLength;
                    paper.LastPrintTime = paperInfo.LastPrintTime;
                    paper.Length = paperInfo.Length;
                    paper.PaperConsumptionLength = paperInfo.PaperConsumptionLength;
                    paper.PaperEndArea = paperInfo.PaperEndArea;
                    paper.paperFace = entities.paperFace.FirstOrDefault(p => p.PaperFaceID == paperInfo.PaperFaceID);
                    paper.PaperTitleArea = paperInfo.PaperTitleArea;
                    paper.paperType = entities.paperType.FirstOrDefault(p => p.PaperTypeID == paperInfo.PaperTypeID);
                    paper.printer = entities.printer.FirstOrDefault(p => p.PrinterID == paperInfo.PrinterID);
                    paper.Remark = paperInfo.Remark;
                    paper.Status = paperInfo.Status;
                    paper.PaperYieldArea = paperInfo.ValidationArea;
                    paper.Width = paperInfo.Width;
                    entities.AddToPaperInfo(paper);
                    entities.SaveChanges();
                }
                catch (Exception ex)
                {
                    ret = -1;
                }
            }
            return ret;
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public int Update(object o)
        {

            int ret = 0;
            PaperInfoModel paperInfo = (PaperInfoModel)o;
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                try
                {
                    PaperInfo paper = entities.PaperInfo.FirstOrDefault(p => p.PrintPaperId == paperInfo.PrintPaperId);
                    paper.LastPrintTime = paperInfo.LastPrintTime;
                    paper.Length = paperInfo.Length;
                    paper.LastBordId = paperInfo.LastBordId;
                    paper.LastLength = paperInfo.LastLength;
                    paper.Status = paperInfo.Status;
                    paper.PaperConsumptionLength = paperInfo.PaperConsumptionLength;
                    paper.PaperYieldArea = paperInfo.ValidationArea;
                    paper.PaperBreakArea = paperInfo.InvalidationArea;
                    entities.SaveChanges();
                }
                catch (Exception ex)
                {
                    ret = -1;
                }
            }
            return ret;
        }

        public int Delete(object o)
        {
            throw new NotImplementedException();
        }

        #endregion

    }
}
